package priv.dk.springboot.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import priv.dk.springboot.domain.entity.Employee;
import priv.dk.springboot.service.EmployeeService;
import priv.dk.springboot.util.RespBean;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmployeeController {
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @GetMapping
    @ResponseBody
    public List<Employee> getAll() {
        return employeeService.getAll();
    }

    @PostMapping("/login")
    @ResponseBody
    public RespBean<Employee> login(HttpSession session, @RequestBody(required = false) Employee employee) {
        // 检查是否已经登录
        Object sessionStatus = session.getAttribute("employee");
        if (sessionStatus != null) return RespBean.ok(((Employee) sessionStatus));

        // 参数校验
        if (employee == null) return RespBean.error("非法参数");
        if (employee.getUsername() == null || employee.getPassword() == null) return RespBean.error("非法参数");

        Employee emp = employeeService.login(employee);
        // 存储登录状态
        session.setAttribute("employee", emp);
        System.out.println("JSESSIONID = " + session.getId());
        return RespBean.ok(emp);
    }

    @GetMapping("/logout")
    @ResponseBody
    public RespBean<String> logout(HttpSession session) {
        session.removeAttribute("employee");
        return RespBean.ok();
    }

    @PostMapping
    @ResponseBody
    public RespBean<String> addEmp(HttpSession session, @RequestBody(required = false) Employee employee) {
        // 参数校验
        if (employee == null) return RespBean.error("非法参数");
        // 参数配置
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        // 获取当前登录的用户
        Employee currentEmployee = (Employee) session.getAttribute("employee");
        employee.setCreateUser(currentEmployee.getId());
        employee.setUpdateUser(currentEmployee.getId());
        employeeService.addEmp(employee);
        return RespBean.ok();
    }

    @PutMapping
    @ResponseBody
    public RespBean<String> modifyEmp(HttpSession session, @RequestBody(required = false) Employee employee) {
        // 参数校验
        if (employee == null) return RespBean.error("非法参数");
        // 参数配置
        Employee currentEmployee = (Employee) session.getAttribute("employee");
        employee.setUpdateUser(currentEmployee.getId());
        employee.setUpdateTime(new Date());
        // 执行修改业务
        employee = employeeService.modifyEmp(employee);
        // 更新session缓存
        session.setAttribute("employee", employee);
        return RespBean.ok();
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public RespBean<String> removeEmp(HttpSession session, @PathVariable Integer id) {
        // 参数校验
        if (id == null) return RespBean.error("非法参数");
        // 权限验证
        Employee employee = (Employee) session.getAttribute("employee");
        if (employee.getId() != 1) return RespBean.error("你没有权限删除员工账户");
        employeeService.removeEmp(id);
        return RespBean.ok();
    }

    @GetMapping("/disable/{id}")
    public RespBean<String> disableEmp(@PathVariable Integer id){
        if (id == null) return RespBean.error("非法参数");
        employeeService.disableEmp(id);
        return RespBean.ok();
    }
}
